//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    spinShow: false,
    fixedPaper: [],
    pushPaper: [],
    taskList: [],
    queryParam: {
        pageNum: 1,
        pageSize: 5,
      },
      total: 1,
      tableData: [],
      queryParam1: {
        examId: "",
      },
  },

  onLoad: function(options) {
    this.setData({
      spinShow: true
    });
    this.search(true)
  },
  onPullDownRefresh() {
    this.setData({
      spinShow: true
    });
    if (!this.loading) {
      this.setData({
        ['queryParam.pageNum']: 1
      });
      this.search(true)
    }
  },
  onReachBottom() {
    if (!this.loading && this.data.queryParam.pageNum < this.data.total) {
      this.setData({
        loadMoreLoad: true,
        loadMoreTip: '正在加载'
      });
      this.setData({
        ['queryParam.pageNum']: this.data.queryParam.pageNum + 1
      });
      this.search(false)
    }
  },
  search: function(override) {

    let _this = this;
    app.formPostToBody('/api/wx/student/dashboard/index/v1', _this.data.queryParam).then(res => {
      _this.setData({
        spinShow: false
      });
      wx.stopPullDownRefresh()
      if (res.code === 1) {
        const re = res.response
        _this.setData({
          ['queryParam.pageNum']: re.pageNum,
          tableData: override ? re.list : this.data.tableData.concat(re.list),
          total: re.pages
        });
        if (re.pageNum >= re.pages) {
          this.setData({
            loadMoreLoad: false,
            loadMoreTip: '暂无数据'
          });
        }
      }else if(res.code == 401){
        wx.qy.login({
            success(wxres) {
                console.log("code是"+wxres.code)

              if (wxres.code) {
                app.formGet('/inApi/qywx/student/auth/appletLogin', {
                  "code": wxres.code
                }).then(res => {
                  if (res.code == 1) {
                    wx.setStorageSync('token', res.response.token)
                    wx.reLaunch({
                      url: '/pages/index/index',
                    });
                  }
                  else {
                    _this.message(res.message, '您无该系统的权限，请联系系统管理员')
    
                        wx.reLaunch({
                      url: '/pages/403/index',
                    });
                  }
                }).catch(e => {
                  _this.message(e, 'error')
                })  
              } else {
                _this.message(res.errMsg, 'error')
              }
            }
          })

      }else{
          
      }
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })
   
  },

  getDetail:function(event){
    console.log("event是")
    console.log(event)
    this.setData({
        ['queryParam1.examId']: event.currentTarget.dataset.examid,

    })
    app.formPost('/api/wx/student/exampaper/select/' +  event.currentTarget.dataset.id, this.data.queryParam1)
    .then(res => {
        this.setData({
        spinShow: false
      });
      if (res.code === 1) {

        wx.navigateTo({
            url: '../../pages/exam/do/index?id='+ event.currentTarget.dataset.id+'&examId='+event.currentTarget.dataset.examid,
          })
      }else if(res.code === 305){
      
        wx.showToast({
            title: "无权限继续考试!",
          });
 
              
              
        }
      
    }).catch(e => {
      _this.setData({
        spinShow: false
      });
      app.message(e, 'error')
    })

    
  },
})